<template>
  <d2-container>

    <template slot="header">

      <el-form :gutter="24" ref="search-box" :model="mainQuery" label-width="auto" label-position="left" :inline="true">

       
        <el-form-item label="统计时间" size="mini">
          <el-date-picker
            v-model="mainQuery.settleTimeRange"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

      </el-form>

      <div class="search-btn-box">
        <div class="left">
           <el-button class="filter-item"  type="default" @click="resetMainData" :loading="mainTable.loading" size="mini">导出</el-button>
          <el-button class="filter-item" icon="el-icon-search" type="primary" @click="fetchMainData" :loading="mainTable.loading" size="mini">查询</el-button>
        </div>
        <div class="right">
        </div>
      </div>
    </template>

    <!--主列表数据-->
    <d2-crud
      ref="d2Crud"
      :options="mainTable.options"
      :index-row="mainTable.indexRow"
      :columns="mainTable.columns"
      :data="mainTable.list"
      :selection-row="mainTable.allowSelect"
      :loading="mainTable.loading"
      @current-change="onMainCurrentChange"
      @selection-change=""
    ></d2-crud>

    <template slot="footer">
      <el-pagination
        @size-change="handleMainSizeChange"
        @current-change="handleMainPageChange"
        :current-page="mainTable.pagination.currentPage"
        :page-sizes="mainTable.pagination.pageSizes"
        :page-size="mainTable.pagination.pageSize"
        :page-count="mainTable.pagination.pageCount"
        :total="mainTable.pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        :background="true"
        >
      </el-pagination>
    </template>

  </d2-container>
</template>

<script>

import util from '@/libs/util';

import ApiMain from '@/api/settle.operatetrade';

export default {
  mixins: [
  ],
  components: {
  },
  data () {
    return {

      accountTypeOptions: [],
      typeOptions       : [],
      stepOptions       : [],

      mainQueryRaw: null,
      mainQuery: {
        terNo      : '',
        accountType: '',
        step       : '',
      },

      mainTable: {
        loading: false,
        options: {
          border             : true,
          stripe             : true,
          highlightCurrentRow: true,
          emptyText          : '暂无数据',
          size               : 'mini',
        },
        indexRow: {
          title:'序号',
          width: "80",
          align:'center'
        },
        indexRow: null,
        allowSelect: false,
        columns: [
          {title: '统计月份', key: 'terNo', align: 'center', showOverflowTooltip: true},
          {title: '商户号', key: 'strAccountType', align: 'center', showOverflowTooltip: true},
     
          {title: '交易金额', key: 'createTime', align: 'center', showOverflowTooltip: true, minWidth: 100,},
          {title: '成功交易金额', key: 'strStep', align: 'center', showOverflowTooltip: true},
          {title: '结算币种', key: 'checkTime', align: 'center', showOverflowTooltip: true, minWidth: 100,},
          {title: '交易笔数', key: 'checkRemark', align: 'center', showOverflowTooltip: true},
          {title: '成功交易笔数', key: 'reCheckTime', align: 'center', showOverflowTooltip: true, },
          {title: '风控阻挡笔数', key: 'reCheckRemark', align: 'center', showOverflowTooltip: true},
          {title: '拒付笔数', key: 'moneyTime', align: 'center', showOverflowTooltip: true, minWidth: 100,},
          {title: '交易成功率', key: 'moneyRemark', align: 'center', showOverflowTooltip: true},
             {title: '退款率', key: 'moneyTime', align: 'center', showOverflowTooltip: true, minWidth: 100,},
          {title: '拒付率', key: 'moneyRemark', align: 'center', showOverflowTooltip: true},
          
        ],
        list: [],
        pagination: {
          currentPage: 1,
          pageSize   : 50,
          total      : 0,
          pageSizes  : [50, 100, 200]
        },
        current: null,
      },

    }
  },
  mounted () {

    this.mainQueryRaw = Object.copy(this.mainQuery);

    var re = this.fetchOptions()
    .finally(() => {
      this.resetMainData();
    });

  },

  methods: {

    renderMainData (re) {

      re.list.forEach(item => {
        item.strAccountType = util.tool.strFromOptions(item.accountType, this.accountTypeOptions, '-');
        item.strType = util.tool.strFromOptions(item.type, this.typeOptions, '-');
        item.strStep = util.tool.strFromOptions(item.step, this.stepOptions, '-');
      });

      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleMainSizeChange (val) {
      this.mainTable.pagination.pageSize = val;
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData()
    },
    handleMainPageChange (val) {
      this.mainTable.pagination.currentPage = val;
      this.fetchMainData()
    },

    buildMainQuery () {

      var query = {
        page  : this.mainTable.pagination.currentPage,
        limit : this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    resetMainData () {

      this.mainQuery = Object.copy(this.mainQueryRaw);
      this.fetchMainData();
    },

    async fetchOptions () {

      var re = [
        { value: 1, label: '待审核', },
        { value: 2, label: '审核驳回', },
        { value: 3, label: '审核通过', },
        { value: 4, label: '复核驳回', },
        { value: 5, label: '复核通过', },
        { value: 6, label: '已出款', },
      ];
      this.stepOptions = re;

      var re = await ApiMain.getAccountTypeOptions();
      this.accountTypeOptions = re;

      var re = await ApiMain.getTypeOptions();
      this.typeOptions = re;

    },

    async fetchMainData () {

      this.mainTable.loading = true;

      try {
        var re = await ApiMain.withdrawList(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) {}


      this.mainTable.loading = false;

    },

    onMainCurrentChange (now, old) {
      this.mainTable.current = now;
    },

  }
}
</script>

<style>

</style>
